package com.etoak.common.redis.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Component
public class RedisService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * String 类型 setex 命令
     *
     * @param key
     * @param value
     * @param expire
     * @param timeUnit
     */
    public void setex(String key, String value, long expire, TimeUnit timeUnit) {
        stringRedisTemplate.opsForValue().set(key, value, expire, timeUnit);
    }

    /**
     * string 类型 get 命令
     */
    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * hash hset
     *
     * @param key
     * @param field
     * @param value
     */
    public void hset(String key, String field, String value) {
        stringRedisTemplate.opsForHash().put(key, field, value);
    }

    /**
     * hash hvals
     */
    public List<String> hvals(String key) {
        return stringRedisTemplate.opsForHash().values(key)
                .stream().map(Object::toString).toList();
    }

    /**
     * hash hdel
     *
     * @param key
     * @param fields
     */
    public void hdel(String key, String... fields) {
        stringRedisTemplate.opsForHash().delete(key, fields);
    }

}


